package com.zhixin.project.biz.message.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.zhixin.framework.aspectj.lang.annotation.Log;
import com.zhixin.framework.aspectj.lang.enums.BusinessType;
import com.zhixin.project.biz.message.domain.BizMessage;
import com.zhixin.project.biz.message.service.IBizMessageService;
import com.zhixin.framework.web.controller.BaseController;
import com.zhixin.framework.web.domain.AjaxResult;
import com.zhixin.common.utils.poi.ExcelUtil;
import com.zhixin.framework.web.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * messageController
 * 
 * @author zhixin
 * @date 2024-05-20
 */
@Controller
@RequestMapping("/biz/message")
public class BizMessageController extends BaseController
{
    private String prefix = "biz/message";

    @Autowired
    private IBizMessageService bizMessageService;

    @RequiresPermissions("biz:message:view")
    @GetMapping()
    public String message()
    {
        return prefix + "/message";
    }

    /**
     * 查询message列表
     */
    @RequiresPermissions("biz:message:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BizMessage bizMessage)
    {
        startPage();
        List<BizMessage> list = bizMessageService.selectBizMessageList(bizMessage);
        return getDataTable(list);
    }

    /**
     * 导出message列表
     */
    @RequiresPermissions("biz:message:export")
    @Log(title = "message", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BizMessage bizMessage)
    {
        List<BizMessage> list = bizMessageService.selectBizMessageList(bizMessage);
        ExcelUtil<BizMessage> util = new ExcelUtil<BizMessage>(BizMessage.class);
        return util.exportExcel(list, "message数据");
    }

    /**
     * 新增message
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存message
     */
    @RequiresPermissions("biz:message:add")
    @Log(title = "message", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BizMessage bizMessage)
    {
        return toAjax(bizMessageService.insertBizMessage(bizMessage));
    }

    /**
     * 修改message
     */
    @RequiresPermissions("biz:message:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        BizMessage bizMessage = bizMessageService.selectBizMessageById(id);
        mmap.put("bizMessage", bizMessage);
        return prefix + "/edit";
    }

    /**
     * 修改保存message
     */
    @RequiresPermissions("biz:message:edit")
    @Log(title = "message", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BizMessage bizMessage)
    {
        return toAjax(bizMessageService.updateBizMessage(bizMessage));
    }

    /**
     * 删除message
     */
    @RequiresPermissions("biz:message:remove")
    @Log(title = "message", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bizMessageService.deleteBizMessageByIds(ids));
    }

    @RequestMapping("/addMessage")
    @ResponseBody
    public Object addMessage(HttpServletRequest request){
        BizMessage message = new BizMessage();
        message.setCustname(request.getParameter("custname"));
        message.setCustcity(request.getParameter("custcity"));
        message.setCustphone(request.getParameter("custphone"));
        message.setCustemail(request.getParameter("custemail"));
        message.setCustmessage(request.getParameter("custmessage"));
        message.setCreatetime(new Date());
        bizMessageService.insertBizMessage(message);
        return "OK";
    }
}
